package com.hu.zhcc.shiro.controller.ClentSide;

import com.hu.zhcc.shiro.entity.Appointment.AReservation;
import com.hu.zhcc.shiro.entity.Appointment.Entry;
import com.hu.zhcc.shiro.entity.Appointment.ReservationSetmeal;
import com.hu.zhcc.shiro.entity.Appointment.TSetmeal;
import com.hu.zhcc.shiro.entity.bo.HealthIntervention.FormulateScheme;
import com.hu.zhcc.shiro.entity.bo.MedicalExaminationReport;
import com.hu.zhcc.shiro.entity.bo.Member;
import com.hu.zhcc.shiro.service.Appointment.ReserService;
import com.hu.zhcc.shiro.service.Appointment.ReservationSetmealService;
import com.hu.zhcc.shiro.service.Appointment.TSetmealService;
import com.hu.zhcc.shiro.service.HealthIntervention.FormulateSchemeService;
import com.hu.zhcc.shiro.service.MERService;
import com.hu.zhcc.shiro.service.MemberService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 前台预约
 */
@RestController
@RequestMapping("CBooking")
@CrossOrigin
public class BookingController {
    //是否登录
    private boolean isLogin = false;
    //当前登录对象
    private Member m = null;
    //套餐
    @Autowired
    private TSetmealService tSetmealService;
    //预约
    @Autowired
    private ReserService reserService;
    //会员
    @Autowired
    private MemberService memberService;
    //套餐预约关联
    @Autowired
    private ReservationSetmealService reservationSetmealService;
    //报告
    @Autowired
    private MERService merService;
    //干预
    @Autowired
    private FormulateSchemeService formulateSchemeService;
    //查询所有套餐
    @GetMapping("")
    public List<TSetmeal> getAllTSetmeal() {
        List<TSetmeal> allTSetmeal = tSetmealService.findAllTSetmeal("");
        return allTSetmeal;
    }

    //查询套餐详情
    @GetMapping("/CBookingDetails/{id}")
    public List<Entry> getAllTSetmealDetails(@PathVariable int id) {
        List<Entry> entries = tSetmealService.allEntry(id);
        return entries;
    }

    //新增预约
    @PostMapping("/addAReservation")
    public int addAReservation(@RequestBody AReservation obj, int id) {
        String time = obj.getTionApptime().substring(0, 10);
        String time2 = obj.getTionBirth().substring(0, 10);
        obj.setTionApptime(time);
        obj.setTionBirth(time2);
//        System.out.println(obj);
        int i1 = reserService.insertSelective(obj);
//        String r = obj.getTionApptime().replace("-", "");
        obj.setTionNumber(m.getFileNumber());
        ReservationSetmeal reservationSetmeal = new ReservationSetmeal(1, obj.getId(), id);
        int i = reservationSetmealService.addReservationSetmeal(reservationSetmeal);
        System.out.println(obj);
        return reserService.updateByPrimaryKeySelective(obj);
    }

    //查询预约记录
    @GetMapping("bookingRecord")
    public List<AReservation> bookingRecord() {
//        List<AReservation> allAReserByPhoneNumber = reserService.findAllAReserByPhoneNumber(m.getPhoneNumber());
        List<AReservation> allAReserByFileNumber = reserService.findAllAReserByFileNumber(m.getFileNumber());
        System.out.println(allAReserByFileNumber);
        return allAReserByFileNumber;
    }

    //预约记录详情
    @GetMapping("bookingRecordDetails")
    public List<Entry> bookingRecordDetails(int id) {
        List<Entry> aLlEntryByReservationId = tSetmealService.findALlEntryByReservationId(id);
        return aLlEntryByReservationId;
    }

    //预约记录详情
    @GetMapping("bookingRecordDetails2")
    public String bookingRecordDetails2(int id) {
        String name = tSetmealService.findTSetmealByReservationId(id).getName();
        return name;
    }

    //取消预约
    @DeleteMapping("cancelBooking")
    public int cancelBooking(int id) {
        int i = reserService.deleteByPrimaryKey(id);
        return i;
    }

    //健康档案
    @GetMapping("healthRecord")
    public Member healthRecord() {
        if (m != null) {
            Member memberByPhoneNumber = memberService.getMemberByPhoneNumber(m.getPhoneNumber(), m.getPassword());
            return memberByPhoneNumber;
        }
        return null;
    }

    //添加家族史
    @PostMapping("addFamilyHistory")
    public Member addFamilyHistory(String str, String newStr) {
        System.out.println(str + newStr);
        Member member = memberService.getMemberByPhoneNumber(m.getPhoneNumber(), m.getPassword());
        member.setFamilyHistory(member.getFamilyHistory() + "\n" + newStr);
        int i = memberService.updateMember(member);
        return member;
    }

    //添加疾病史
    @PostMapping("addMedicalHistory")
    public Member addMedicalHistory(String newStr) {
        Member member = memberService.getMemberByPhoneNumber(m.getPhoneNumber(), m.getPassword());
        member.setMedicalHistory(member.getMedicalHistory() + "\n" + newStr);
        int i = memberService.updateMember(member);
        return member;
    }

    //添加过敏史
    @PostMapping("addAllergicHistory")
    public Member addAllergicHistory(String newStr) {
        Member member = memberService.getMemberByPhoneNumber(m.getPhoneNumber(), m.getPassword());
        member.setAllergicHistory(member.getAllergicHistory() + "\n" + newStr);
        int i = memberService.updateMember(member);
        return member;
    }
    //报告查询
    @GetMapping("reportQuery")
    public List<MedicalExaminationReport> reportQuery() {
        if (m != null) {
            List<MedicalExaminationReport> allMedicalExaminationReport = merService.findAllMedicalExaminationReportByFileNumber(m.getFileNumber());
            return allMedicalExaminationReport;
        }
        return null;
    }
    //干预方案
    @GetMapping("interposeScheme")
    public List<FormulateScheme> interposeScheme(){
        FormulateScheme formulateByPopId = formulateSchemeService.findFormulateByPopId(m.getId());
        formulateByPopId.setExerciseStartTime(formulateByPopId.getExerciseStartTime()+"-"+formulateByPopId.getExerciseEndTime());
        formulateByPopId.setStarttime(formulateByPopId.getStarttime().substring(0, 10)+"到"+formulateByPopId.getEndtime().substring(0, 10));
        List<FormulateScheme> arr = new ArrayList<>();
        arr.add(formulateByPopId);
        return arr;
    }
    //登录
    @PostMapping("login")
    public int login(String name, String pwd) {
        System.out.println(name + pwd);
        Member memberByPhoneNumber = memberService.getMemberByPhoneNumber(name, pwd);
        if (memberByPhoneNumber != null) {
            isLogin = true;
            m = memberByPhoneNumber;
            return 0;
        }
        return 1;
    }

    //判断是否登录
    @GetMapping("isLogin")
    public boolean isLogin() {
        return isLogin;
    }

    //退出登录
    @GetMapping("quit")
    public int quit() {
        isLogin = false;
        m = null;
        return 1;
    }
}
